package com.kunlun.common.api;

import com.kunlun.common.model.monitor.SystemDataModel;
import com.kunlun.common.utils.JsonUtil;
import com.kunlun.common.utils.MonitorUtil;
import com.kunlun.common.utils.ResponseUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 公共API控制器
 */
@RestController
@RequestMapping("/commonApi")
@ConditionalOnProperty(name = "kunlun.monitor.enable", havingValue = "true", matchIfMissing = true)
public class CommonApiController {

    private Logger log = LogManager.getLogger();

    @Autowired
    private MonitorUtil monitorUtil;

    @Value("${spring.application.name}")
    private String serviceName;

    @RequestMapping(value = "monitorSystem", method = RequestMethod.GET)
    public Object monitorSystem() {
        try {
            SystemDataModel systemData = new SystemDataModel(serviceName, monitorUtil.getSysInfo(), monitorUtil.getCpuInfo(), monitorUtil.getMemoryInfo(), monitorUtil.getDiskInfo(), monitorUtil.getJvmInfo(), monitorUtil.getNetIoInfo());
            log.info("monitorSystem systemData => " + JsonUtil.toJsonString(systemData));
            return ResponseUtil.successResponse(systemData);
        } catch (Exception e) {
            log.error("采集系统信息失败", e);
            return ResponseUtil.failedResponse("采集系统信息失败", e.getMessage());
        }
    }
}
